package com.example.robertluozizhao.baseframedemo.util.reflect;

import android.content.ContentValues;

import com.example.robertluozizhao.baseframedemo.util.LogUtils;
import com.example.robertluozizhao.baseframedemo.util.reflect.model.ClsMember;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * 类映射工具类
 * Created by Administrator on 2016/7/7 0007.
 */
public class ReflectUtils {
    private static final String TAG = "ReflectUtils";
    public static final String TYPE_DATE = "java.util.Date"; //时间类型
    public static final String TYPE_STRING = "java.lang.String";//字符串类型
    public static final String TYPE_UUID = "java.util.UUID";//UUID类型
    public static final String TYPE_INT = "int";
    public static final String TYPE_LONG = "long";
    public static final String TYPE_FLOAT = "float";
    public static final String TYPE_DOUBLE = "double";
    public static final String TYPE_SHORT = "short";
    public static final String TYPE_BOOLEAN = "boolean";

    public static final String[] TYPES = new String[]{TYPE_DATE, TYPE_STRING, TYPE_UUID, TYPE_INT, TYPE_LONG, TYPE_FLOAT, TYPE_FLOAT, TYPE_DOUBLE, TYPE_SHORT, TYPE_BOOLEAN};

    /**
     * 获取各属性值
     *
     * @param object
     * @return
     */
    public static ContentValues getPerpertys(Object object) {
        ContentValues contentValues = new ContentValues();
        for (ClsMember member : ReflectUtils.getObjectMembers(object.getClass(), 0)) {
            try {
                Class cls = object.getClass();
                int i = member.getLevel();
                while (i-- != 0) {
                    cls = cls.getSuperclass();
                }
                Field field = cls.getDeclaredField(member.getName());
                field.setAccessible(true);
                if (member.getType().equals(ReflectUtils.TYPE_BOOLEAN)) {
                    contentValues.put(member.getName(), field.getBoolean(object) == true ? 1 : 0);
                    LogUtils.d(TAG, "HEHE1" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_SHORT)) {
                    contentValues.put(member.getName(), field.getShort(object));
                    LogUtils.d(TAG, "HEHE2" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_LONG)) {
                    contentValues.put(member.getName(), field.getLong(object));
                    LogUtils.d(TAG, "HEHE3" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_INT)) {
                    contentValues.put(member.getName(), field.getInt(object));
                    LogUtils.d(TAG, "HEHE4" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_FLOAT)) {
                    contentValues.put(member.getName(), field.getFloat(object));
                    LogUtils.d(TAG, "HEHE5" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_DOUBLE)) {
                    contentValues.put(member.getName(), field.getDouble(object));
                    LogUtils.d(TAG, "HEHE6" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_DATE)) {
                    Object date = field.get(object);
                    contentValues.put(member.getName(), (Long) date.getClass().getDeclaredMethod("getTime").invoke(date));
                    LogUtils.d(TAG, "HEHE7" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_STRING)) {
                    Object string = field.get(object);
                    contentValues.put(member.getName(), string == null ? "" : (String) string.getClass().getDeclaredMethod("toString").invoke(string));
                    LogUtils.d(TAG, "HEHE8" + member.toString());
                } else if (member.getType().equals(ReflectUtils.TYPE_UUID)) {
                    Object uuid = field.get(object);
                    contentValues.put(member.getName(), uuid == null ? "" : (String) uuid.getClass().getDeclaredMethod("toString").invoke(uuid));
                    LogUtils.d(TAG, "HEHE9" + member.toString());
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                LogUtils.d(TAG, e.toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                LogUtils.d(TAG, e.toString());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                LogUtils.d(TAG, e.toString());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                LogUtils.d(TAG, e.toString());
            }
        }
        return contentValues;
    }


    /**
     * 获得对象属性
     *
     * @param cls
     * @param level 默认为0，若为1，则获取的是父类的属性
     * @return
     */
    public static List<ClsMember> getObjectMembers(Class<?> cls, int level) {
        List<ClsMember> members = new ArrayList<ClsMember>();
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                ClsMember member = new ClsMember();
                member.setName(field.getName());
                member.setType(field.getType().getName());
                member.setAccessible(field.isAccessible());
                member.setLevel(level);
                members.add(member);
            }
        }
        if (!cls.getSuperclass().getSimpleName().equals("Object")) {
            members.addAll(getObjectMembers(cls.getSuperclass(), level + 1));
        }
        return members;
    }

    /**
     * 获得类属性
     *
     * @param cls
     * @param level 默认为0，若为1，则获取的是父类的属性
     * @return
     */
    public static List<ClsMember> getClassMembers(Class<?> cls, int level) {
        List<ClsMember> members = new ArrayList<ClsMember>();
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            if (Modifier.isStatic(field.getModifiers())) {
                ClsMember member = new ClsMember();
                member.setName(field.getName());
                member.setType(field.getType().getName());
                member.setAccessible(field.isAccessible());
                member.setLevel(level);
                members.add(member);
            }
        }
        if (!cls.getSuperclass().getSimpleName().equals("Object")) {
            members.addAll(getObjectMembers(cls.getSuperclass(), level + 1));
        }
        return members;
    }
}
